package cn.hairui.blog.mapper;

import cn.hairui.blog.model.Album;
import cn.hairui.blog.model.AlbumImg;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.BaseMapper;

import java.util.List;
import java.util.Map;

/**
 * @author lihairui
 * @version V1.0
 * @date: 2019/7/5 22:09
 */
@Mapper
public interface AlbumMapper extends BaseMapper<Album> {

    @Select("SELECT * FROM ALBUMIMG WHERE ALBUM_ID IN (SELECT ID FROM ALBUM WHERE SHOW_FLAG='Y') ORDER BY ADD_TIME DESC,ID DESC LIMIT 1")
    AlbumImg findLatestImg();


    @Select("SELECT * FROM ALBUM WHERE SHOW_FLAG='Y' ORDER BY CREATE_TIME DESC")
    List<Album> findGust();

    @Select("SELECT * FROM ALBUM WHERE CREATOR=#{username} OR SHOW_FLAG='Y' ORDER BY CREATE_TIME DESC")
    List<Album> findManager(String username);

    @Select("SELECT * FROM ALBUM ORDER BY CREATE_TIME DESC")
    List<Album> list();

    @Select("SELECT COUNT(1) FROM ALBUMIMG WHERE ALBUM_ID=#{id}")
    Integer countImg(Integer id);

    @Select("SELECT * FROM ALBUM WHERE ID=#{id}")
    Album find(int id);

    @Select("SELECT * FROM ALBUMIMG WHERE ALBUM_ID=#{id} ORDER BY ID DESC")
    List<AlbumImg> findImgsByAlbuId(int id);

    @Select("SELECT T1.ID AS id,T1.NAME AS name,T1.SHOW_IMG AS showImg,CASE WHEN T1.SHOW_FLAG='Y' THEN 'checked' ELSE '' END AS showFlag, CREATE_TIME AS createTime,(SELECT T2.NICKNAME FROM USER T2 WHERE T1.CREATOR=T2.USERNAME) AS creator,(SELECT COUNT(1) FROM ALBUMIMG T3 WHERE T3.ALBUM_ID=T1.ID) AS imgNum,T1.LOCATION AS location FROM ALBUM T1 ORDER BY T1.ID ASC")
    List<Map> listMgr();

    @Update("UPDATE ALBUM SET SHOW_FLAG=#{flag} WHERE ID=#{id}")
    void updateIsShow(int id, String flag);

    @Select("SELECT  ID AS pid,IMG_PATH AS src,IMG_PATH AS thumb,CASE WHEN IMG_NAME=NULL THEN '' ELSE IMG_NAME END AS alt FROM ALBUMIMG WHERE ALBUM_ID=#{id}")
    List<Map> findImgsJsonByAlbuId(int id);

    @Delete("DELETE FROM ALBUM WHERE ID=#{id}")
    void deleteById(int id);


    /*@Select("SELECT * FROM ALBUM ORDER BY ID DESC")
    public List<Album> queryAlbumList();

    @Insert("INSERT INTO ALBUM " +
            "(NAME,SHOW_IMG,SHOW_FLAG,INTRODUCTION,CREATE_TIME,CREATOR) " +
            "VALUES" +
            "(#{name},#{showImg},#{showFlag},#{introduction},#{createTime},#{creator})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public int addAlbum(Album gallerys);

    @Insert("INSERT INTO GALLERY_IMG " +
            "(GALLERY_ID,IMG_NAME,IMG_PATH,INTRODUCTION)" +
            "VALUES" +
            "(#{galleryId},#{imgName},#{imgPath},#{introduction})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public int addAlbumImg(AlbumImg galleryImg);

    @Select("SELECT COUNT(1) FROM GALLERY_IMG WHERE GALLERY_ID=#{gallery_id}")
    public Integer queryAlbumImgCountByAlbumId(Integer gallery_id);

    //list出入顺序  asc保证展示最新在最前面
    @Select("SELECT * FROM GALLERY_IMG WHERE GALLERY_ID=#{gallery_id} ORDER BY ID ASC LIMIT #{maxSize}")
    public List<AlbumImg> queryAlbumImgByAlbumIdLimited(Integer gallery_id, Integer maxSize);

    @Select("SELECT * FROM GALLERY_IMG WHERE GALLERY_ID=#{gallery_id} ORDER BY ID DESC")
    public List<AlbumImg> queryAlbumImgList(Integer gallery_id);

    @Select("SELECT * FROM ALBUM WHERE ID=#{id}")
    public Album queryAlbumById(Integer id);

    @Update("UPDATE ALBUM SET NAME=#{name},SHOW_IMG=#{showImg},SHOW_FLAG=#{showFlag},INTRODUCTION=#{introduction} WHERE ID=#{id}")
    public int updateAlbum(Album gallerys);

    @Select("SELECT * FROM GALLERY_IMG WHERE ID=#{id}")
    public AlbumImg queryAlbumImgById(Integer id);

    @Select("SELECT * FROM GALLERY_IMG WHERE ID!=#{id} AND IMG_PATH=#{imgPath}")
    public AlbumImg queryAlbumImgByImgPathExceptId(Integer id, String imgPath);

    @Delete("DELETE FROM GALLERY_IMG WHERE ID=#{id}")
    public int deleteAlbumImgById(Integer id);

    @Select("SELECT * FROM GALLERY_IMG WHERE GALLERY_ID=#{galleryId} oRDER BY ID DESC LIMIT 1")
    public AlbumImg queryAlbumImgByAlbumIdFirst(Integer galleryId);

    @Update("UPDATE ALBUM SET SHOW_IMG=#{newImg} WHERE ID=#{galleryId}")
    public int setShowImgByAlbumId(Integer galleryId, String newImg);

    @Delete("DELETE FROM ALBUM WHERE ID=#{id}")
    public int deleteAlbumById(Integer galleryId);

    @Select("SELECT COUNT(1) FROM GALLERY_IMG WHERE IMG_PATH=#{imgName}")
    public int queryAlbumImgByImgPath(String imgName);

    @Select("SELECT * FROM ALBUM WHERE SHOW_FLAG='Y'")
    public List<Album> queryAlbumShowList();

    @Select("SELECT * FROM ALBUM WHERE CREATOR=#{username}")
    public List<Album> queryAlbumListByOwner(String username);

    @Select("SELECT * FROM ALBUM WHERE CREATOR=#{username} OR SHOW_FLAG='Y'")
    public List<Album> queryAlbumListByOwnerAndShow(String username);*/
}
